<template>
  <div class="article-detail">
    <el-row :gutter="20">
      <!-- 侧边目录导航 -->
      <el-col :xs="0" :sm="0" :md="3" :lg="4" :xl="3">
        <div class="toc-wrapper">
          <el-card class="toc-card" shadow="hover">
            <template #header>
              <div class="toc-header">
                <span>文章目录</span>
              </div>
            </template>
            <div class="toc-container">
              <div v-if="!titles.length" class="no-content">
                <h4>无内容</h4>
              </div>
              <div v-for="(title, index) in titles" :key="index"
                   :class="['toc-item', `toc-${title.tag}`, { 'active': activeHeading === title.id }]"
                   @click="scrollTo(title.id)">
                {{ title.text }}
              </div>
            </div>
          </el-card>
        </div>
      </el-col>

      <!-- 文章内容区 -->
      <el-col :xs="24" :sm="24" :md="16" :lg="16" :xl="17">
        <el-card class="article-card" shadow="always" v-loading="loading" element-loading-text="正在加载文章内容..."
                 element-loading-background="rgba(255, 255, 255, 0.8)">
          <!-- 文章头部信息 -->
          <template #header>
            <div class="card-header">
              <el-image :src="imgUrl + articleList.articleImg" fit="cover" class="article-banner"
                        :preview-src-list="[imgUrl + articleList.articleImg]" loading="lazy">
                <template #error>
                  <div class="image-error">
                    <el-icon>
                      <Picture />
                    </el-icon>
                    <span>加载失败</span>
                  </div>
                </template>
              </el-image>

              <div class="header">
                <!-- 分类标签 -->
                <el-tag class="category-tag" effect="light">
                  {{ articleList.categoryName }}
                </el-tag>

                <h1>{{ articleList.articleTitle }}</h1>

                <el-row :gutter="20" style="position:absolute;bottom: 10px;color:aliceblue">
                  <el-col :span="3">
                    <div style="display: flex;">
                      <el-icon size="20">
                        <Timer />
                      </el-icon>
                      <div style="min-width:80px;max-width:1500px;">
                        {{ $filters.timeFilter(articleList.createTime) }}
                      </div>
                    </div>
                  </el-col>
                  <el-col :span="2">
                    <div style="display: flex;">
                      <el-icon size="20">
                        <View />
                      </el-icon>
                      {{ articleList.viewCount }}
                    </div>
                  </el-col>
                </el-row>
              </div>
            </div>
          </template>
          <!-- AI摘要卡片 -->
          <el-card class="summary-card" shadow="never">
            <template #header>
              <div class="summary-header">
                <el-icon class="ai-icon">
                  <MagicStick />
                </el-icon>
                <span>AI内容摘要</span>
              </div>
            </template>
            <div class="summary-content">
              <span class="loader-text" v-if="articleList.aiExcerpt">{{ articleList.aiExcerpt }}</span>
              <!-- <div v-if="articleList.aiExcerpt">{{ articleList.aiExcerpt }}</div> -->
              <div v-else class="empty-summary">暂无AI生成摘要</div>
            </div>
          </el-card>

          <!-- 文章内容区 -->
          <div class="article-content">
            <!-- 富文本内容渲染 -->
            <div v-if="isRichTextContent" v-html="articleList.articleContent" class="quill-content"></div>

            <!-- Markdown内容渲染 -->
            <div v-else class="markdown-content">
              <md-preview :modelValue="articleList.articleContent" :preview-theme="previewTheme" />
            </div>
          </div>

          <!-- 文章底部信息 -->
          <div class="article-footer" v-if="articleList">
            <div class="article-info">
              <p>发布于 {{ $filters.timeFilter(articleList.createTime) }}</p>
              <p v-if="articleList.updateTime && articleList.updateTime !== articleList.createTime">
                最后更新于 {{ $filters.timeFilter(articleList.updateTime) }}
              </p>
              <p v-if="articleList.editorType" class="editor-type-info">
                <el-tag size="small" type="info">
                  {{ articleList.editorType === 'markdown' ? 'Markdown编辑器' : '富文本编辑器' }}
                </el-tag>
              </p>
            </div>
          </div>
        </el-card>

        <!-- 评论区 -->
        <div class="comments-section">
          <div class="comments-container">
            <PingLun :id="props.id" />
          </div>
        </div>
      </el-col>


      <!-- 右侧边栏 - 联系方式和近期文章 -->
      <el-col :xs="0" :sm="0" :md="3" :lg="4" :xl="4">
        <div class="right-sidebar">
          <WebsiteConfig :keys="[
                        'siteName',
                        'avatarImg',
                        'emil',
                        'qqh'
                    ]">
            <template #default="{ config }">
              <!-- 联系方式卡片 -->
              <el-card class="sidebar-card contact-card" shadow="hover">
                <template #header>
                  <div class="sidebar-card-header">
                    <el-icon>
                      <Message />
                    </el-icon>
                    <span>联系方式</span>
                  </div>
                </template>
                <div class="contact-content">
                  <div class="contact-item">
                    <el-icon>
                      <Message />
                    </el-icon>
                    <span v-if="!config">邮箱：加载中....</span>
                    <span v-else>邮箱：{{ config.emil }}</span>
                  </div>
                  <div class="contact-item">
                    <el-icon>
                      <ChatDotRound />
                    </el-icon>
                    <span v-if="!config">QQ：加载中....</span>
                    <span v-else>QQ：{{ config.qqh }}</span>
                  </div>
                  <div class="contact-item">
                    <el-icon>
                      <Promotion />
                    </el-icon>
                    <span v-if="!config">创作者：加载中....</span>
                    <span v-else>创作者：{{ config.siteName }}</span>
                  </div>
                  <div class="contact-qrcode">
                    <el-image v-if="!config" src="/api/placeholder/200/200" alt="微信二维码"></el-image>
                    <el-image v-else :src="config.avatarImg"></el-image>
                  </div>
                </div>
              </el-card>
            </template>
          </WebsiteConfig>



          <!-- 近期文章卡片 -->
          <el-card class="sidebar-card recent-articles-card" shadow="hover">
            <template #header>
              <div class="sidebar-card-header">
                <el-icon>
                  <Reading />
                </el-icon>
                <span>近期文章</span>
              </div>
            </template>
            <div class="recent-articles-content">
              <div v-if="!homeList.length" class="loading-state">
                <el-skeleton :rows="3" animated />
              </div>
              <el-empty v-else-if="homeList.length === 0" description="暂无近期文章" />
              <transition-group name="article-list" tag="ul" class="recent-articles-list" v-else>
                <li v-for="(item, index) in homeList" :key="item.id" class="recent-article-item"
                    @click="openDetail(item.id, item.categoryId)">
                  <!-- 封面图容器 -->
                  <div class="cover-container">
                    <el-image :src="imgUrl + item.articleImg" fit="cover" class="article-cover"
                              loading="lazy">
                      <template #error>
                        <div class="cover-error">
                          <el-icon>
                            <Picture />
                          </el-icon>
                        </div>
                      </template>
                    </el-image>
                    <div class="article-badges">
                      <el-tag v-if="item.isTop" class="top-tag" effect="dark" type="danger"
                              size="small">置顶</el-tag>
                      <el-tag class="new-tag" effect="plain" type="success"
                              size="small">新</el-tag>
                    </div>
                  </div>

                  <!-- 文章信息 -->
                  <div class="article-info">
                    <div class="recent-article-title" :title="item.articleTitle">
                      {{ item.articleTitle }}
                    </div>
                    <div class="article-summary" v-if="item.articleSummary">
                      {{ truncateSummary(item.articleSummary) }}
                    </div>
                    <div class="meta-info">
                      <div class="meta-item">
                        <el-icon>
                          <Clock />
                        </el-icon>
                        <span>{{ formatDate(item.createTime) }}</span>
                      </div>
                      <div class="meta-item">
                        <el-icon>
                          <View />
                        </el-icon>
                        <span>{{ formatViewCount(item.viewCount) }}</span>
                      </div>
                    </div>
                  </div>
                </li>
              </transition-group>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, h, nextTick, onMounted, onUnmounted, computed } from "vue"
import { getArticle, addReadCount } from "@/api/bk/article.js"
import { listHome } from "@/api/bk/home.js" // 导入近期文章接口
import { ElNotification } from 'element-plus'
import PingLun from "@/components/PingLun/index.vue"
import { Picture, Timer, View, MagicStick, Message, ChatDotRound, Promotion, Reading } from '@element-plus/icons-vue';
import { MdPreview } from 'md-editor-v3';
import 'md-editor-v3/lib/preview.css';
import WebsiteConfig from '@/components/WebsiteConfig/index.vue'  // 导入网站配置组件
import { useRouter } from 'vue-router';
import { ArrowRight } from '@element-plus/icons-vue';


import hljs from 'highlight.js/lib/core';
import javascript from 'highlight.js/lib/languages/javascript';
import python from 'highlight.js/lib/languages/python';
import java from 'highlight.js/lib/languages/java';
import css from 'highlight.js/lib/languages/css';
import html from 'highlight.js/lib/languages/xml';
import bash from 'highlight.js/lib/languages/bash';
import markdown from 'highlight.js/lib/languages/markdown';
import json from 'highlight.js/lib/languages/json';

import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'

hljs.registerLanguage('javascript', javascript);
hljs.registerLanguage('python', python);
hljs.registerLanguage('java', java);
hljs.registerLanguage('css', css);
hljs.registerLanguage('html', html);
hljs.registerLanguage('bash', bash);
hljs.registerLanguage('markdown', markdown);
hljs.registerLanguage('json', json);

const props = defineProps({
  id: {
    type: String,
    required: true
  }
});

const router = useRouter();
const articleList = ref('')
const imgUrl = import.meta.env.VITE_API_BASE_URL
const titles = ref([])
const activeHeading = ref('');
const loading = ref(true);
let headingObserver = null;
const previewTheme = ref('vuepress');

// 近期文章列表
const homeList = ref([]);
const queryParams = ref({
  pageNum: 1,
  pageSize: 5
});
const total = ref(0);

// 计算属性：判断是否为富文本内容
const isRichTextContent = computed(() => {
  if (!articleList.value || !articleList.value.articleContent) return true;

  if (articleList.value.editorType) {
    return articleList.value.editorType === 'rich';
  }

  const htmlRegex = /<[a-z][\s\S]*>/i;
  return htmlRegex.test(articleList.value.articleContent);
});

// 获取文章数据
const getArticleList = () => {
  const id = props.id;
  loading.value = true;
  getArticle(id).then(response => {
    if (response.data.status == '1') {
      articleList.value = response.data;
      nextTick(() => {
        setTimeout(() => {
          applyCodeHighlighting();
          parseArticleHeadings();
          loading.value = false;
        }, 1500);
      });
    } else {
      loading.value = false;
      ElNotification({
        title: '注意',
        duration: 5000,
        message: h('i', { style: 'color: teal' }, '获取文章失败，可能使用了非法字符(*^_^*)'),
      });
    }
  }).catch(() => {
    loading.value = false;
    ElNotification({
      title: '错误',
      type: 'error',
      duration: 5000,
      message: '获取文章数据失败，请稍后重试',
    });
  });
}

const openDetail = (id, categoryId) => {
  if (categoryId != 4) {
    // 新窗口打开
    const url = router.resolve({
      name: 'Detail',
      params: { id } // 传递参数
    }).href;
    window.open(url, '_blank');
  } else {
    return
  }
};

// 格式化日期 (不显示年份)
const formatDate = (dateString) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return `${month}月${day}日`;
};

// 格式化阅读数
const formatViewCount = (count) => {
  if (count < 1000) return count;
  return (count / 1000).toFixed(1) + 'k';
};

// 截断摘要文本
const truncateSummary = (summary) => {
  if (!summary) return '';
  return summary.length > 40 ? summary.substring(0, 40) + '...' : summary;
};


// 获取近期文章列表
const getRecentArticles = () => {
  listHome(queryParams.value).then(response => {
    homeList.value = response.rows;
    total.value = response.total;
  }).catch(() => {
    ElNotification({
      title: '错误',
      type: 'error',
      duration: 3000,
      message: '获取近期文章失败',
    });
  });
}

// 高亮代码块的函数
const applyCodeHighlighting = () => {
  document.querySelectorAll('.quill-content pre code').forEach((block) => {
    try {
      hljs.highlightElement(block);
    } catch (error) {
      console.error('Rich text code highlight failed:', error);
    }
  });
}

// 解析文章标题并创建导航
const parseArticleHeadings = () => {
  if (isRichTextContent.value) {
    parseHtmlHeadings();
  } else {
    setTimeout(() => {
      parseMarkdownHeadings();
    }, 1500);
  }
}

// 解析HTML标题
const parseHtmlHeadings = () => {
  const headings = Array.from(document.querySelectorAll('.article-content .quill-content h1, .article-content .quill-content h2, .article-content .quill-content h3, .article-content .quill-content h4, .article-content .quill-content h5, .article-content .quill-content h6'));

  titles.value = headings.map((element, index) => {
    const id = `heading-${index}`;
    element.id = id;
    element.style.scrollMarginTop = '80px';

    return {
      id,
      text: element.innerText,
      tag: element.tagName.toLowerCase()
    };
  });
}

// 解析Markdown标题
const parseMarkdownHeadings = () => {
  const headings = Array.from(document.querySelectorAll('.markdown-content .md-editor-preview h1, .markdown-content .md-editor-preview h2, .markdown-content .md-editor-preview h3, .markdown-content .md-editor-preview h4, .markdown-content .md-editor-preview h5, .markdown-content .md-editor-preview h6'));

  titles.value = headings.map((element, index) => {
    const id = `heading-${index}`;
    element.id = id;
    element.style.scrollMarginTop = '80px';

    return {
      id,
      text: element.innerText,
      tag: element.tagName.toLowerCase()
    };
  });
}

// 设置标题观察器
const setupHeadingObserver = () => {
  const observer = new MutationObserver((mutations) => {
    if (!isRichTextContent.value) {
      parseMarkdownHeadings();
    }
  });

  const targetNode = document.querySelector('.markdown-content');
  if (targetNode) {
    observer.observe(targetNode, {
      childList: true,
      subtree: true
    });
  }

  return observer;
}

// 滚动到对应标题位置
const scrollTo = (id) => {
  activeHeading.value = id;
  const element = document.getElementById(id);
  if (element) {
    element.scrollIntoView({
      behavior: 'smooth',
      block: 'start'
    });
  }
}

// 增加阅读计数
const incrementReadCount = () => {
  const id = props.id;
  addReadCount(id).then(() => {
    console.log('阅读计数已增加');
  });
}

// 处理滚动事件，更新当前活动标题
const handleScroll = () => {
  if (titles.value.length === 0) return;

  const headings = titles.value.map(title => document.getElementById(title.id));

  // 找到当前在视窗中的标题
  for (let i = 0; i < headings.length; i++) {
    if (!headings[i]) continue;

    const rect = headings[i].getBoundingClientRect();
    if (rect.top >= 0 && rect.top <= 200) {
      activeHeading.value = headings[i].id;
      return;
    }
  }

  // 如果没有标题在视窗内，找最接近顶部的
  let closestHeading = null;
  let minDistance = Infinity;

  headings.forEach(heading => {
    if (!heading) return;

    const rect = heading.getBoundingClientRect();
    const distance = Math.abs(rect.top);

    if (distance < minDistance) {
      minDistance = distance;
      closestHeading = heading;
    }
  });

  if (closestHeading) {
    activeHeading.value = closestHeading.id;
  }
}

onMounted(() => {
  getArticleList();
  getRecentArticles(); // 获取近期文章列表

  nextTick(() => {
    headingObserver = setupHeadingObserver();
  });

  setTimeout(incrementReadCount, 5000);

  window.addEventListener('scroll', handleScroll);
});

onUnmounted(() => {
  window.removeEventListener('scroll', handleScroll);

  if (headingObserver) {
    headingObserver.disconnect();
  }
});
</script>

<style lang="scss">
// html,body{
//     height: 100%;
// }
// 新增摘要卡牌样式
.summary-card {
  margin: 30px 20px 20px;
  border: 1px solid #ebeef5;
  border-radius: 8px;

  :deep(.el-card__header) {
    background-color: #f8f9fa;
    padding: 12px 20px;
    border-bottom: 1px solid #ebeef5;
  }

  .summary-header {
    display: flex;
    align-items: center;
    font-weight: 600;
    color: #2c3e50;

    .ai-icon {
      margin-right: 8px;
      color: #409EFF;
      font-size: 18px;
    }
  }

  .summary-content {
    font-size: 14px;
    line-height: 1.6;
    color: #4a4a4a;
    padding: 12px 15px;
    // 打字机效果
    position: relative;
    overflow: hidden;
    border-right: 3px solid;
    width: 0px;
    animation:
        typewriter 2s steps(10) forwards,
          /* 移除 infinite 和 alternate */
        blink 0.5s steps(10) infinite;
    /* 保持光标持续闪烁 */

    .loader-text {
      font-weight: 700;
      background: linear-gradient(to right, #159957, #155799);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }

    @keyframes typewriter {
      0% {
        width: 0px;
      }

      /* 将 100% 设为终点状态 */
      100% {
        width: 96%;
      }
    }

    @keyframes blink {

      0%,
      25%,
      50%,
      75% {
        border-right-color: rgba(255, 255, 255, .75);
      }

      12.5%,
      37.5%,
      62.5%,
      87.5%,
      100% {
        border-right-color: transparent;
      }
    }
  }
}

/* md-editor-v3 样式覆盖 */
.md-editor-preview {
  padding: 0 !important;
  /* 覆盖默认内边距 */
  text-align: left;
}

/* 原有的 v-md-preview 样式修改为 md-editor-v3 */
.md-editor-preview pre code {
  display: block;
  overflow-x: auto;
  padding: 1em;
  background: #f6f8fa !important;
  border-radius: 6px;
  font-size: 14px !important;
  /* 统一增大代码块字体 */
  line-height: 1.6 !important;
}

// 文章详情整体样式
.article-detail {
  margin: 0.8rem auto;
  max-width: 1800px;
  padding: 0 10px;
}

// 评论区样式
.comments-section {
  .comments-container {
    max-width: 100%;
  }
}

// 目录导航包装器
.toc-wrapper {
  position: sticky;
  top: 70px;
  z-index: 10;
  height: fit-content;
}

// 目录导航卡片
.toc-card {
  border-radius: 8px;
  margin-bottom: 20px;

  @media (max-width: 768px) {
    display: none;
  }
}

// 右侧边栏样式
.right-sidebar {
  position: sticky;
  top: 70px;

  @media (max-width: 1350px) {
    display: none;
  }
}

// 侧边栏卡片通用样式
.sidebar-card {
  margin-bottom: 20px;
  border-radius: 8px;
  padding: 10px;


  :deep(.el-card__header) {
    padding: 12px 15px;
    border-bottom: 1px solid #f0f0f0;
  }

  :deep(.el-card__body) {
    padding: 15px;
  }
}

// 侧边栏卡片头部样式
.sidebar-card-header {
  display: flex;
  align-items: center;
  font-weight: 600;
  font-size: 16px;
  color: #333;

  .el-icon {
    margin-right: 8px;
    color: #409EFF;
  }
}

// 联系方式卡片样式
.contact-card {
  .contact-content {
    padding-top: 10px;

    .contact-item {
      display: flex;
      align-items: center;
      margin-bottom: 12px;
      font-size: 14px;

      .el-icon {
        margin-right: 8px;
        color: #409EFF;
      }
    }

    .contact-qrcode {
      text-align: center;
      margin-top: 15px;

      img {
        width: 120px;
        height: 120px;
        border: 2px solid #1bffc694;
        border-radius: 80px;
      }

      p {
        margin-top: 8px;
        font-size: 14px;
        color: #666;
      }
    }
  }
}

// 近期文章卡片样式
.recent-articles-card {
  margin-bottom: 20px;
  border-radius: 12px;
  overflow: hidden;
  transition: transform 0.3s ease, box-shadow 0.3s ease;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  }

  :deep(.el-card__header) {
    padding: 15px;
    background: linear-gradient(135deg, #f8f9fa, #e9ecef);
    border-bottom: 1px solid #f0f0f0;
  }

  :deep(.el-card__body) {
    padding: 0;
  }

  .sidebar-card-header {
    display: flex;
    align-items: center;
    font-weight: 600;
    font-size: 16px;
    color: #333;

    .el-icon {
      margin-right: 8px;
      color: #409EFF;
      font-size: 18px;
    }
  }

  .recent-articles-content {
    padding: 5px 0;
  }

  .loading-state {
    padding: 15px;
  }

  .recent-articles-list {
    padding: 0;
    margin: 0;
    list-style: none;

    .recent-article-item {
      display: flex;
      padding: 12px 15px;
      border-bottom: 1px solid #f0f0f0;
      cursor: pointer;
      transition: all 0.3s ease;

      &:last-child {
        border-bottom: none;
      }

      &:hover {
        background-color: #f9f9f9;

        .article-cover {
          transform: scale(1.05);
        }

        .recent-article-title {
          color: #409EFF;
        }
      }
    }

    .cover-container {
      position: relative;
      flex-shrink: 0;
      width: 90px;
      height: 70px;
      border-radius: 8px;
      overflow: hidden;
      margin-right: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    .article-cover {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.4s ease;
    }

    .article-badges {
      position: absolute;
      top: 5px;
      left: 5px;
      display: flex;
      gap: 4px;

      .top-tag {
        background-color: rgba(245, 108, 108, 0.9);
        backdrop-filter: blur(4px);
      }

      .new-tag {
        background-color: rgba(103, 194, 58, 0.2);
        backdrop-filter: blur(4px);
        border-color: rgba(103, 194, 58, 0.4);
      }
    }

    .cover-error {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #f5f7fa;
      color: #909399;

      .el-icon {
        font-size: 24px;
      }
    }

    .article-info {
      flex: 1;
      min-width: 0;
      display: flex;
      flex-direction: column;
    }

    .recent-article-title {
      font-size: 15px;
      font-weight: 500;
      line-height: 1.4;
      margin-bottom: 6px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
      color: #303133;
      transition: color 0.3s;
    }

    .article-summary {
      font-size: 12px;
      color: #909399;
      margin-bottom: 6px;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;
      overflow: hidden;
    }

    .meta-info {
      display: flex;
      align-items: center;
      gap: 16px;
      font-size: 12px;
      color: #909399;
      margin-top: auto;

      .meta-item {
        display: flex;
        align-items: center;
        gap: 4px;

        .el-icon {
          font-size: 14px;
        }
      }
    }
  }

  .more-articles {
    text-align: center;
    padding: 10px 0;
    border-top: 1px dashed #eaeaea;
  }
}

/* 列表动画效果 */
.article-list-enter-active,
.article-list-leave-active {
  transition: all 0.5s ease;
}

.article-list-enter-from,
.article-list-leave-to {
  opacity: 0;
  transform: translateY(30px);
}

.article-list-move {
  transition: transform 0.5s ease;
}

.toc-header {
  display: flex;
  justify-content: center;
  font-weight: 600;
  color: #333;
  font-size: 16px;
  padding: 8px 0;
}

.toc-container {
  max-height: calc(100vh - 140px);
  overflow-y: auto;
  padding-right: 5px;
  min-height: 50px;

  .no-content {
    display: flex;
    justify-content: center;
  }

  /* 美化滚动条 */
  &::-webkit-scrollbar {
    width: 4px;
  }

  &::-webkit-scrollbar-track {
    background: #f7f7f7;
  }

  &::-webkit-scrollbar-thumb {
    background: #d8d8d8;
    border-radius: 4px;
  }

  @media (max-width: 768px) {
    max-height: 250px;
  }
}

.toc-item {
  cursor: pointer;
  padding: 8px 12px;
  margin: 4px 0;
  font-size: 14px;
  border-radius: 4px;
  transition: all 0.3s;
  text-align: left;
  line-height: 1.4;

  &:hover {
    background-color: #f5f5f5;
    transform: translateX(5px);
  }

  &.active {
    background-color: #e6f1fc;
    border-left: 3px solid #409eff;
  }
}

// 目录层级缩进
.toc-h2 {
  padding-left: 1em;
}

.toc-h3 {
  padding-left: 2em;
}

.toc-h4 {
  padding-left: 3em;
}

.toc-h5 {
  padding-left: 4em;
  font-size: 13px;
}

.toc-h6 {
  padding-left: 5em;
  font-size: 12px;
}

// 文章卡片样式
.article-card {
  min-width: 870px;
  border-radius: 8px;
  margin-bottom: 20px;
}

/* 保持与原来一致的卡片头部 */
.card-header {
  position: relative;
}

.article-banner {
  width: 100%;
  max-height: 280px;
  /* 保持原来的图片高度 */
}

.header {
  position: absolute;
  background-color: rgba(80, 80, 80, 0.3);
  width: 100%;
  height: 100%;
  top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;

  h1 {
    // 新增标题样式
    width: 80%;
    margin: 0 auto;
    font-size: 0.3rem;
    text-align: center;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    position: relative;
    padding: 0 20px;

    // 响应式调整
    @media (max-width: 768px) {
      font-size: 1.8rem;
      width: 90%;
    }

    @media (max-width: 480px) {
      font-size: 1.5rem;
    }

    // 添加装饰线
    &::after {
      content: '';
      display: block;
      width: 600px;
      height: 3px;
      background: rgba(255, 255, 255, 0.6);
      margin: 15px auto 0;
      border-radius: 2px;
    }
  }

  // 调整原有定位元素
  .category-tag {
    // 保持原有定位
    position: absolute;
    top: 8px;
    left: 8px;
    // 其他样式保持不变...
  }

  .el-row {
    // 保持底部信息定位不变
    position: absolute;
    bottom: 10px;
    width: 100%;
    left: 0;
    padding: 0 20px;
  }
}

/* 保持原来的分类标签样式 */
.category-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  font-size: 14px;
  background: rgba(144, 238, 144, 0.4);
  border: none;
  color: #2c3e50;
  border-radius: 8px 8px 8px 0;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  z-index: 1;
  backdrop-filter: blur(2px);
}

/* 保持原来的标题样式 */
.header h1 {
  color: aliceblue;
}

/* 文章内容样式 */
.article-content {
  display: block !important;
  /* 清除grid布局影响 */
  padding: 20px;

  /* 通用内容样式 */
  p,
  h1,
  h2,
  h3,
  li {
    text-align: left !important;
  }

  pre {
    text-align: left;
    white-space: pre-wrap;
    word-break: break-word;
    padding: 1em;
    border-radius: 8px;
    background-color: #f6f8fa;
    margin: 16px 0;

    code {
      font-family: 'Courier New', monospace;
    }
  }

  /* 富文本特定样式 */
  .quill-content {
    min-height: 400px;
  }

  /* Markdown特定样式 - 为md-editor-v3优化 */
  .markdown-content {
    min-height: 400px;

    .md-editor-preview {
      padding: 0 !important;
      /* 移除默认内边距 */
      min-height: 400px;
      border: none;
      box-shadow: none;

      /* 调整段落间距 */
      p {
        margin: 0.8em 0 !important;
        /* 减少段落间的垂直间距 */
        line-height: 1.6;
      }

      /* 调整列表项间距 */
      ul,
      ol {
        margin: 0.8em 0 !important;
        padding-left: 1.5em !important;
      }

      li {
        margin-bottom: 0.3em !important;
      }

      /* Improved heading visibility */
      h1,
      h2,
      h3,
      h4,
      h5,
      h6 {
        margin-top: 1.2em !important;
        margin-bottom: 0.5em !important;
        font-weight: 600;
        line-height: 1.25;
        color: #2c3e50;
        position: relative;

        /* Visual indicator for headings with IDs */
        &[id]::before {
          content: "";
          display: block;
          height: 80px;
          margin-top: -80px;
          visibility: hidden;
        }
      }

      /* Enhanced code block styling */
      pre {
        margin: 0.8em 0 !important;
        /* 减少代码块的外边距 */
        background-color: #f6f8fa;
        border-radius: 8px;
        overflow: auto;

        code {
          font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
          font-size: 14px !important;
          /* 增大代码块字体 */
          padding: 0;
          background: transparent;
          line-height: 1.6 !important;
        }
      }

      /* Inline code styling */
      code:not([class*="language-"]) {
        padding: 0.2em 0.4em;
        margin: 0;
        font-size: 14px !important;
        /* 增大行内代码字体 */
        background-color: rgba(27, 31, 35, 0.05);
        border-radius: 3px;
        font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
      }

      /* 调整表格样式 */
      table {
        margin: 0.8em 0 !important;
        border-collapse: collapse;
        width: 100%;
        overflow-x: auto;
        display: block;
      }

      table th,
      table td {
        padding: 0.5em 1em !important;
        border: 1px solid #dfe2e5;
      }

      /* 调整引用块样式 */
      blockquote {
        margin: 0.8em 0 !important;
        padding: 0 1em !important;
        color: #6a737d;
        border-left: 0.25em solid #dfe2e5;
      }

      /* 调整水平线样式 */
      hr {
        margin: 1.2em 0 !important;
        height: 1px;
        background-color: #dfe2e5;
        border: none;
      }
    }
  }
}

// QL Syntax 代码块样式
.ql-syntax {
  background-color: #f6f8fa;
  border-radius: 6px;
  padding: 16px;
  margin: 16px 0;
  overflow: auto;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 14px !important;
  /* 增大富文本代码块字体 */
  line-height: 1.6;
}

/* 文章底部信息 */
.article-footer {
  padding: 15px 20px;
  border-top: 1px solid #eaeaea;
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;

}

.article-info {
  color: #666;
  font-size: 14px;

  p {
    margin: 5px 0;
  }

  .editor-type-info {
    margin-top: 10px;
  }
}

/* 图片加载错误样式 */
.image-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 280px;
  background-color: #f5f7fa;
  color: #909399;

  .el-icon {
    font-size: 48px;
    margin-bottom: 10px;
  }
}

/* 清除element-plus卡片默认内边距 */
.el-card__body {
  padding: 0;
}

.el-card__header {
  padding: 0;
}

/* 加载状态自定义样式 */
.el-loading-mask {
  border-radius: 8px;
}
</style>